package myTree;

public class MaxWidth {
    final static int MaxLevel = 100;        //最大层次
    static int[] w = new int[MaxLevel];        //存放每一层结点个数

    public static int width(MyBTree bt)    //求二叉树的宽度
    {
        width1(bt.b, 1);
        int ans = 0;
        for (int i = 1; i < MaxLevel; i++)        //求w中最大元素
            if (ans < w[i])
                ans = w[i];
        return ans;
    }

    private static void width1(BTNode<Character> t, int h) {
        if (t == null) return;
        w[h]++;                    //第h层的结点个数增1
        width1(t.lchild, h + 1);            //遍历左子树
        width1(t.rchild, h + 1);            //遍历右子树
    }
}
